/*
 *  SPAGL Copyright (C) 2008 Sam Pottinger
 *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser 
 *  General Public License as published by the Free Software Foundation; either version 2.1 of the License, or 
 *  (at your option) any later version. This library is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License along with this library; if not, 
 *  write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

package spagswt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Scale;
import spagcore.SpaglCoreContainer;
import spagcore.SpaglCoreEvent;
import spagcore.SpaglCoreSlider;

/**
 * A Slider (org.eclipse.swt.widgets.Slider) that conforms to the SpaglCoreSlider interface
 * @author Sam Pottinger
 */
public class SpaglSlider implements SpaglSwtWidget, SpaglCoreSlider {

	private Scale innerWidget;
	private SpaglCoreContainer parentWidget;
	private int orientation;
	public static final int VERTICAL = SWT.VERTICAL;
	public static final int HORIZONTAL = SWT.HORIZONTAL;

	/**
	 * Creates a slider using the default layout behavior
	 * @param newParent The parent widget that will hold this object
	 * @param min The minimum integer value that can be selected in this widget
	 * @param max The maximum integer value that can be selected in this widget
	 * @param newOrientation The integer representation of the orientation of this widget. Either the class constant VERTICAL or HORIZONTAL
	 */
	public SpaglSlider(SpaglCoreContainer newParent, int min, int max, int newOrientation)
	{
		innerWidget = new Scale(((SpaglContainer)newParent).getComposite(), newOrientation);
		innerWidget.setMinimum(min);
		innerWidget.setMinimum(max);
		parentWidget = newParent;
		orientation = newOrientation;
	}
	
	/**
	 * Creates a slider using the specified layout parameters
	 * @param newParent The parent widget that will hold this object
	 * @param min The minimum integer value that can be selected in this widget
	 * @param max The maximum integer value that can be selected in this widget
	 * @param orientation The integer representation of the orientation of this widget. Either the class constant VERTICAL or HORIZONTAL
	 * @param parameters The string parameters to be used in placing this widget in the layout manager
	 */
	public SpaglSlider(SpaglCoreContainer newParent, int min, int max, int orientation, String parameters)
	{
		innerWidget = new Scale(((SpaglContainer)newParent).getComposite(), orientation);
		innerWidget.setMinimum(min);
		innerWidget.setMinimum(max);
		parentWidget = newParent;
		parentWidget.placeWidget(this, parameters);
	}
	
	/**
	 * Gets this widget as a SWT Slider (org.eclipse.swt.widgets.Slider) object
	 * @return A SWT Scale widget
	 */
	public Scale getNativeWidget() {
		return innerWidget;
	}

	/**
	 * Disposes this widget through SWT
	 */
	public void dispose() {
		innerWidget.dispose();
	}

	/**
	 * Gets the parent widget that is holding this object
	 * @return The container holding this widget as a {@link SpaglCoreContainer}
	 */
	public SpaglCoreContainer getContainer() {
		return parentWidget;
	}

	/**
	 * Gets the height in pixels of this widget
	 * 
	 * @return The vertical size of this widget
	 */
	public int getHeight() {
		return innerWidget.getSize().y;
	}

	/**
	 * Gets the width in pixels of this widget
	 * 
	 * @return The horizontal size of this widget
	 */
	public int getWidth() {
		return innerWidget.getSize().x;
	}

	/**
	 * Sets the size of the object in pixels
	 * 
	 * @param width the horizontal size in size x
	 * @param height the vertical size in size y
	 */
	public void setSize(int width, int height) {
		innerWidget.setSize(width, height);
	}

	/**
	 * Gets the maximum selectable value in this widget
	 * @return An integer representation of the maximum value selectable in this object
	 */
	public int getMax() {
		return innerWidget.getMaximum();
	}

	/**
	 * Gets the minimum selectable value in this widget
	 * @return An integer representation of the minimum value selectable in this object
	 */
	public int getMin() {
		return innerWidget.getMinimum();
	}

	/**
	 * Gets the orientation of this widget
	 * @return An integer representing if this widget is horizontal or vertical. The implementing class provides these values.
	 */
	public int getOrientation() {
		return orientation;
	}

	/**
	 * Gets the value that is currently selected by the user
	 * @return The integer (int) value that that widget is currently set to 
	 */
	public int getValue() {
		return innerWidget.getSelection();
	}

	/**
	 * Sets the maximum value available for the user to select in this widget
	 * @param newMax The new largest value that can be selected in this object
	 */
	public void setMax(int newMax) {
		innerWidget.setMaximum(newMax);
	}

	/**
	 * Sets the minimum value available for the user to select in this widget
	 * @param newMin The new smallest value that can be selected in this object
	 */
	public void setMin(int newMin) {
		innerWidget.setMinimum(newMin);
	}

	/**
	 * Sets the value currently selected in this widget
	 * @param newValue The new integer represented in this object
	 */
	public void setValue(int newValue) {
		innerWidget.setSelection(newValue);
	}
	
	/**
	 * Adds an event listener to this check box
	 * 
	 * @param eventID The ID of the event to be listened for
	 * @param method The (string) name of the method to be called when the event is fired
	 * @param o The object where the method should be fired
	 */
	public void addEventListener(int eventID, String method, Object o) {
		try{
			switch(eventID)
			{
				case SpaglEvent.VALUE_CHANGED_EVENT: {innerWidget.addListener(SWT.Selection, new SpaglEventListener(o.getClass().getMethod(method, SpaglCoreEvent.class), o, SpaglEvent.VALUE_CHANGED_EVENT)); break;}
				default: throw new IllegalArgumentException("Event not supported by this widget");
			}}
		catch (SecurityException e) {e.printStackTrace();} 
		catch (NoSuchMethodException e) {e.printStackTrace();}
		
	}
	
}
